package march.firstmarch22.march01;

import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;

public class Reducer01 extends Reducer<Text,Text, NullWritable,Text> {
    @Override
    protected void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
        int count = 0;
        int sum = 0;
        int total = 0;
        for (Text value : values) {
            String[] split = value.toString().split(",");
            int length = split.length;

            // 取出一条记录中的所有学生成绩，需要考虑split长度
            for (int i = length - 1; i > 1; i--) {
                total += Integer.parseInt(split[i]);
            }
            //每门课参考人数
            count++;
            //每门课参考人次
             sum += length-2;
        }
        
        long avg = Long.parseLong(total/sum+"");

        String res = "课程名："+key.toString()+","
                +"参考人数："+count+","
                +"课程平均分"+avg;
        
        context.write(NullWritable.get(),new Text(res));
    }
}
